package com.khyr.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.dao.RoleDao;
import com.khyr.entity.Role;
import com.khyr.feign.UserFeign;
import com.khyr.service.RoleService;
import com.khyr.vo.R;
import com.khyr.vo.RoleVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private HttpServletRequest request;

    // 查询所有角色
    public R<List<Role>> selectAllRole() {
        List<Role> roles = roleDao.selectList(null);

        if (roles != null){
            return new R<>(200,"查询成功", roles);
        }

        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<Role> findRolesByUserId(Long userId) {
        if (userId != null){
            Role role = roleDao.selectById(userId);
            return new R<>(200,"查询成功", role);
        }
        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<Page<Role>> findRoleByCond(RoleVo roleVo) {
        //分页
        Page<Role> page = new Page<>(roleVo.getPageNum(), roleVo.getPageSize());
        //模糊查询，使用StringUtils.hasText
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(roleVo.getRoleName())){
            queryWrapper.like("role_name", roleVo.getRoleName());
        }
        if (StringUtils.hasText(roleVo.getRoleCode())){
            queryWrapper.like("role_code", roleVo.getRoleCode());
        }
        if (StringUtils.hasText(roleVo.getStatus())){
            queryWrapper.like("status", roleVo.getStatus());
        }
        Page<Role> rolePage = roleDao.selectPage(page, queryWrapper);
        if (rolePage != null){
            return new R<>(200,"查询成功", rolePage);
        }
        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<Integer> addRole(Role role) {
        if (role != null){
            //设置创建时间
            role.setCreateTime(new Date());
            String token = request.getHeader("token");
            role.setCreateBy(userFeign.getUserInfo(token).getData().getUserName());
            role.setUpdateBy(userFeign.getUserInfo(token).getData().getUserName());
            int insert = roleDao.insert(role);
            if (insert > 0){
                return new R<>(200,"添加成功", insert);
            }
        }
        return new R<>(500,"添加失败", null);
    }

    @Override
    public R<Integer> updateRole(Role role) {
        if (role != null){
            int update = roleDao.updateById(role);
            if (update > 0){
                return new R<>(200,"修改成功", update);
            }
        }
        return new R<>(500,"修改失败", null);
    }

    @Override
    @Transactional
    public R<Integer> deleteRoleByRoleIds(List<Long> ids) {
        if (ids != null){
            int delete = roleDao.deleteIds(ids);
            if (delete > 0){
                return new R<>(200,"删除成功", delete);
            }
        }
        return new R<>(500,"删除失败", null);
    }

    @Override
    public R<Role> getRoleByRoleId(Long id) {
        if (id != null){
            Role role = roleDao.selectById(id);
            return new R<>(200,"查询成功", role);
        }
        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<Integer> saveRoleUser(Long userId, List<Long> roleIds) {
        if (userId != null){
            //先删除
            roleDao.deleteId(userId);
            for (Long roleId : roleIds) {
                roleDao.saveById(userId, roleId);
            }
            return new R<>(200,"修改成功", 1);
        }
        return new R<>(500,"修改失败", null);
    }

    @Override
    public R<Integer> saveRoleMenu(Long roleId, List<Long> menuIds) {
        if (roleId != null){
            //先删除
            roleDao.deleteMenu(roleId);
            for (Long menuId : menuIds) {
                roleDao.saveMenu(roleId, menuId);
            }
            return new R<>(200,"修改成功", 1);
        }
        return null;
    }


}
